package org.zxyj.manage.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zxyj.core.constants.Keys;
import org.zxyj.core.entity.Pagination;
import org.zxyj.core.entity.Result;
import org.zxyj.core.web.controller.BaseController;
import org.zxyj.manage.entity.CoursePaperCrop;
import org.zxyj.manage.service.ICoursePaperService;
import org.zxyj.manage.service.ICourseTemplateService;
import org.zxyj.manage.service.IMarkingPaperService;

/**
 * 主观题阅卷管理控制类
    * @ClassName: MarkingPaperSubjectiveController
    * @author liuyong
    * @date 2016年9月21日
 */
@Controller
@RequestMapping("/manage/markingPaper/subjective")
public class MarkingPaperSubjectiveController extends BaseController{
    @Autowired
	private ICourseTemplateService courseTemplateService;
    @Autowired
    private IMarkingPaperService markingPaperService;
    @Autowired
    private ICoursePaperService coursePaperService;
    
    @RequestMapping({"","/","/index"})
	public ModelAndView index(ModelAndView mv) {
		mv.setViewName(Views.LIST_VIEW);
		Map<String, Object> criteria = new HashMap<String, Object>();
		//查询每个题组阅卷进度情况
		String batchId = getParaValue("batchId");
		String gradeNo = getParaValue("gradeNo");
		String courseNo = getParaValue("courseNo");
		String tmName = getParaValue("tmName");
        criteria.put("batchId",batchId);
        criteria.put("gradeNo",gradeNo);
        criteria.put("courseNo",courseNo);
        criteria.put("tmName",tmName);
		Pagination page = (Pagination) getRequestAttr("pagination");
		Pagination pagination = this.markingPaperService.findMarkingPaperSubjectiveGroupPage(criteria, page);
		mv.addObject(Keys.KEY_PAGINATION, pagination);
		mv.addObject("batchs", courseTemplateService.getExamBatchs(criteria));
		mv.addObject("batchId", batchId);
		mv.addObject("gradeNo", gradeNo);
		mv.addObject("courseNo", courseNo);
		mv.addObject("tmName", tmName);
		return mv;
    }
    
    @RequestMapping("/markingHandleUI")
    public ModelAndView markingHandleUI(ModelAndView mv){
    	mv.setViewName(Views.DETAIL_VIEW);
    	Map<String, Object> criteria = new HashMap<String, Object>();
    	String areaId = getParaValue("areaId");
    	criteria.put("areaId", areaId);
    	Pagination page = (Pagination) getRequestAttr("pagination");
        page.setPageSize(Pagination.PAGESIZE1);
        if(getParaValue("current") !=null){
        	page.setCurrent(Integer.parseInt(getParaValue("current")));
        }
        Pagination pagination = this.markingPaperService.findSubjectiveDetailByAreaIdPage(criteria, page);
		mv.addObject(Keys.KEY_PAGINATION, pagination);
		this.markingPaperService.initSubjectiveTmxx(criteria);
		
		Long pageNumber = (long) pagination.getCurrent();//页码
        Long totalPage = (long) pagination.getTotalPages();//总页数
        mv.addObject("current" , pageNumber);
        mv.addObject("totalPage" , totalPage);
        mv.addObject("nextPage" , pageNumber==totalPage ? pageNumber : pageNumber+1);
        mv.addObject("previousPage" , pageNumber==1 ? pageNumber : pageNumber-1);
        mv.addObject("areaId", areaId);
    	return mv;
    }
    
    class Views {
		/**
		 * 列表视图
		 */
		public static final String LIST_VIEW = "/manage/markingPaper/subjective_list";
		
		/**
		 * 阅卷视图
		 */
		public static final String DETAIL_VIEW = "/manage/markingPaper/subjective_detail";
    }
}
